<template>
  <page-layout>
    <div slot="toolbar">

      <el-button size="small" round type="primary" @click="newVideo">新增视频</el-button>
    </div>
    <div class="card-header">视频管理</div>
    <el-card class="box-card">
      <el-card class="box-card">
        <el-form label-width="80px" inline>
          <el-form-item label="标题">
            <el-input v-model="searchVideoCondition.title" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="refresh(1)">查询</el-button>
          </el-form-item>
        </el-form>
      </el-card>
          <el-card class="box-card">
            <el-table
              :data="videoList.content" border stripe
              style="width: 100%">
              <el-table-column
                prop="id"
                label="编号"
                width="55">
              </el-table-column>
              <el-table-column prop="title" label="标题">
              </el-table-column>
              <el-table-column prop="image" label="视频" width="210">
                <template slot-scope="scope">
                  <image-cover v-if="scope.row.cover" :thumb.sync="scope.row.cover" width="96px" height="63px" size-type="contain"></image-cover>
                  <span v-else>暂时无图</span>
                </template>
              </el-table-column>
              <el-table-column prop="num" label="题目数" ></el-table-column>
              <el-table-column
                prop="count"
                label="答题次数">
              </el-table-column>


              <el-table-column
                prop="passRate"
                label="及格率">
                <template slot-scope="scope">
                    <span>{{scope.row.passRate}}</span>
                </template>
              </el-table-column>

              <el-table-column
                prop="score"
                label="总积分">
              </el-table-column>
              <el-table-column label="操作" width="220">
                <template slot-scope="scope">
                  <el-button size="mini" type="text"   @click="goVideoQuestion(scope.row)">练习题管理</el-button>
                  <el-button size="mini" type="text"   @click="editVideo(scope.row)">编辑</el-button>
                  <el-button size="mini" type="text"   @click="deleteVideo(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="mt10">
              <el-pagination
                background
                layout="prev, pager, next"
                :total="videoList.totalElements"
                :page-size="videoList.size"
                :current-page.sync="currentPage" @current-change="getPageChange">
              </el-pagination>
            </div>
          </el-card>
    </el-card>

    <el-dialog :visible.sync="showVideoEditor" title="添加/编辑 视频" width="50%">
      <video-editor @close="onVideoEditorClose" :is-close="showVideoEditor"></video-editor>
    </el-dialog>
  </page-layout>
</template>
<script>
import PageLayout from "@/components/page-layout";
import { mapGetters } from "vuex";
import VideoEditor from "./video-editor";
import ImageCover from "@/components/image-cover";
export default {
  created() {
    this.refresh();
  },
  mounted() {},
  data() {
    return {
      currentPage: 1,
      showVideoEditor: false
    };
  },
  computed: {
    ...mapGetters({
      videoList: "videoList",
      searchVideoCondition: "searchVideoCondition"
    })
  },
  methods: {
    goVideoQuestion(row) {
      console.log(row.id);
      this.$router.push({
        name: "videoQuestion",
        query: { videoId: row.id }
      });
    },

    newVideo() {
      this.$store.commit("INIT_CURRENT_VIDEO");
      this.showVideoEditor = true;
    },
    editVideo(row) {
      this.$store.dispatch("getVideoS", row.id).then(res => {});
      this.showVideoEditor = true;
    },
    deleteVideo(row) {
      this.$confirm("确认删除当前视频吗?", "提示", {})
        .then(() => {
          this.$store.dispatch("deleteVideoS", { id: row.id }).then(ret => {
            this.refresh();
            this.$notify({
              title: "删除成功",
              type: "success"
            });
          });
        })
        .catch(() => {});
    },
    getPageChange(val) {
      var tem = Object.assign(
        { pageIndex: val - 1 },
        this.searchVideoCondition
      );
      this.$store.dispatch("getVideoPageS", tem).then(ret => {});
    },
    refresh() {
      this.getPageChange(this.currentPage);
    },
    onVideoEditorClose() {
      this.showVideoEditor = false;
      this.refresh();
    }
  },
  components: {
    PageLayout,
    VideoEditor,
    ImageCover
  }
};
</script>
<style>
</style>
